<?php

namespace App\HttpController\Api;
use EasySwoole\Http\Message\Status;

use App\Model\BaseModel;

use App\Think\Org\Token;

class Demo extends ApiBase
{
    protected $noNeedLogin = ['*'];

    public function index(){


        log_to(['test'=>'测试日志'], 'test');

        $user = $this->auth->getUserinfo();
    	//\EasySwoole\RedisPool\Redis::getInstance()->register('redis',new \EasySwoole\Redis\Config\RedisConfig());
        // $redis = \EasySwoole\Pool\Manager::getInstance()->get('redis')->getObj();
    	//$redis = \EasySwoole\RedisPool\Redis::defer('redis');
        // echo rand_ip();
    	// $this->dbRedis()->set('name','asdfasdf');
    	// echo $this->dbRedis()->get('name');

        // Token::set('xxxx1',1);
        // Token::set('xxxx2',2);
        // Token::set('xxxx3',3);
        // Token::set('xxxx31',3);
        // Token::set('xxxx32',3);

        // Token::get('xxxx3');

        // Token::del('xxxx2');

        // print_r(Token::tokens(3));

        // print_r(Token::clear(3));

        // print_r(Token::tokens(3));

        // $cache = \App\Think\Cache::init();
        // $key = $cache->set("user",'user1111');
        // $val =  $cache->get('user');
        // var_dump($cache->get('tt'));
        // if(!$cache->get('tt')) echo '不存在';
        // echo db_get_field('fa_user', "id=1", 'nickname');
        // $this->writeJson( Status::CODE_OK, ['user'=>''] );
        return $this->success("success", ['user'=>$user]);
    }

    public function test(){
        $db = new BaseModel();

        $data = [];
        $db = \App\Think\Db::getInstance();
        $res = $db->getRow('fa_user',"id=1");
        $arr = $db->getAll('fa_user',"1");
        
        $inc_data = $db->addRow('fa_user',['username'=>'hahhaa', 'password'=>'123123']);
        $up_data  = $db->editRow('fa_user', ['username'=>'111', 'password'=>'111'], "id=1");
        $join_data = $db->getJoin('fa_user u', 'u.*', 'fa_game_log g', 'u.id=g.user_id', 1);
        $up_data  = $db->del('fa_user', "id=8");
        // $data = $db->getRow('fa_user', "id=3")->toArray();
        // print_r($data);
        // $builder = new \EasySwoole\Mysqli\QueryBuilder();
        // $data = Model::create()->func(function ($builder){
        //     $builder->where('id',1)->get('fa_user');
        // });

        $this->writeJson( Status::CODE_OK, ['data'=>$res, 'arr'=>$arr, 'insert'=>$inc_data, 'update'=>$up_data, 'join'=>$join_data] );
    }

    public function test2(){
        // $db = new BaseModel();
        // $user = $db->getRow('fa_admin', ['id'=>1]);
        // $user = $this->db()->where('id', 1)->getOne('fa_user');
        $db = $this->db();
        $db->queryBuilder()->get('fa_user');
        $data = $db->execBuilder();

        $this->writeJson( Status::CODE_OK, ['val'=>$user] );
    }

    public function redis(){
        // $this->dbRedis()->set('name','asdfasdf');
        // $name = $this->dbRedis()->get('name');

        // $redis = \EasySwoole\RedisPool\Redis::defer('redis');
        $redis = \App\Think\Redis::init();
        $redis->set('a', 12);
        echo $redis->get('a');
        // var_dump( $redis->get('a'));

        // $redis = \EasySwoole\Pool\Manager::getInstance()->get('redis')->getObj();
        // if(!$res = $redis->get("name")){
        //     $redis->set("name","zq");
        //     $redis->expire("name",10);
        // }
        // $res = $redis->get("name");
        // \EasySwoole\Pool\Manager::getInstance()->get('redis')->recycleObj($redis);
        //回收

        $this->writeJson( Status::CODE_OK, ['data'=>''] );
    }

    public function orm(){
        // $orm = \App\Think\Db::init();
        // $data['find'] = $this->db()->table('fa_user')->field('id,username')->where("id=".rand(0,10000))->find();
        // $data['find2'] = $this->db()->table('fa_user')->field('id,username')->where("id=1")->value('username');
        // $data['select'] = $this->db()->table('fa_version')->field('*')->where("id IN(1,2)")->select();
        // $data['select2'] = $this->db()->table('fa_user')->field('nickname')->where("id IN(1)")->select();
        // echo 'lastsql:'.$orm->getLastSql();
        // try {
        //     $data['join'] = $orm->table('fa_user u')->join('fa_trade t', 'u.id=t.user_id')->field('*')->where("u.id IN(1)")->select();
        // } catch (Exception $e) {
        //     echo $e->getMessage();
        // }

        // db('user')->field('id,username')->where("id=".rand(0,100))->find();
        
        db('user')->field('id')->where("id=".rand(0,100))->find();

        $count = db('user')->field('count(*) count')->count();

        // $user_id = rand(0,100);
        $res = db_get_row('user', "id=".rand(332,355));

        // go(function () {
        //     $user_id = rand(0,100);
        //     $res = db_get_row('user', "id=".rand(0,100));
        //     $this->res = $res;
        //     $this->writeJson( Status::CODE_OK, ['data'=>$res] );
        // });


        // $data['insertReplace'] = $orm->table('fa_user')->addReplace(['username'=>'1_'.rand(0,1000000)],['nickname'=>'ddr','game_user'=>'rrr']);
        // $data['insertReplace'] = $orm->table('fa_user')->addReplace(['username'=>'123123'],['nickname'=>'ddr','game_user'=>'rrr']);

        // $data['insert'] = $orm->name('version')->insert(['oldversion'=>'hahahaha', 'newversion'=>'ddr']);

        // $data['update'] = $orm->table('fa_user')->where("id=14")->update(['username'=>3333]);
        // $data['delete'] = $orm->table('fa_user')->where("id=15")->delete();
        $this->writeJson( Status::CODE_OK, ['data'=>$res, 'count'=>$count] );
    }

    public function cache(){
        $cache = \App\Think\Redis::init();
        $key = $cache->set("user",'user1111');
        $val =  $cache->get('user');

        $this->writeJson( Status::CODE_OK, ['data'=>$val] );
    }

}
